'''
Created on 15-Jul-2010

@author: dexen
'''
import os
import time
import random

from dexen.system.base import task
from dexen.system.base.individual import Individual    

import constants

def eval_daylight(rad):
    rad_path = os.path.join(os.getcwd(),"radiancebasefile")
    filename  = "overhangschema"
    rad.create_rad(rad_path, filename)
    
    hea_path = os.path.join(os.getcwd(), "daysimbasefile")
    hea_bin_dir = "/home/dexen/daysim/bin/"
    rad.execute_hea(hea_path, filename, hea_bin_dir)
    result = rad.eval_hea()
    average = sum(result)/(len(result))
    return average

def eval_daylight_rad(rad):
    rad_path = os.path.join(os.getcwd(),"radiancebasefile")
    filename  = "overhangschema"
    rad.execute_rad(rad_path, filename)
    result = rad.eval_rad("illuminance")
    average = sum(result)/(len(result))
    return average

def evaluate(ind):
    ind.score_daylight = eval_daylight_rad(ind.phen_daylight)
    ind.archive_data("score_daylight.txt", ind.score_daylight)

def evaluate_test(ind):
    assert isinstance(ind, Individual)
    time.sleep(random.randint(1,2))
    ind.score_daylight = random.randint(30, 1000)
    ind.archive_data("score_daylight.txt", ind.score_daylight)
    
class EvalDaylightTask(task.Task):
    def __init__(self):
        pass
    
    def __execute__(self, inds=[]):
        for ind in inds:
            evaluate(ind)
        return inds
    
    def __isvalid__(self, ind):
        return ind.phen_daylight != None and ind.score_daylight == None and ind.score_energy == None
    
